<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/29
 * Time: 14:15
 */

namespace App\Services;

use App\Model\AdminModel;
use EasySwoole\Jwt\Jwt;
class AdminService
{
    /**
     * 创建管理员
     */
    public function createAdmin($param){
        try{
            #校验用户名是否重复
            if(AdminModel::create()->get(['username'=>$param['username'],'app_id'=>$param['app_id']])){
                return ReponseCodeService::CODE_ALREAD_EXISTS;
            }
            #入库
            $adminData['app_id']   = $param['app_id'];
            $adminData['username'] = $param['username'];
            $adminData['status'] = $param['status'];
            $adminData['password_hash'] = $this->pwdEncode($param['password']);
            if(AdminModel::create()->data($adminData)->save()){
                return ReponseCodeService::CODE_OK;
            }
            return ReponseCodeService::CODE_FAILE;
        }catch(\Throwable $t){
            return ReponseCodeService::CODE_DB_ERROR;
        }
    }

    /**编辑信息
     * @param $param
     */
    public function edit($param){
        try{
            #校验用户名是否重复
            if(AdminModel::create()->get(['username'=>$param['username'],'app_id'=>$param['app_id']])){
                return ReponseCodeService::CODE_ALREAD_EXISTS;
            }
            #入库
            $adminData['app_id']   = $param['app_id'];
            $adminData['username'] = $param['username'];
            $adminData['status'] = $param['status'];
            $adminData['password_hash'] = $this->pwdEncode($param['password']);
            if(AdminModel::create()->data($adminData)->save()){
                return ReponseCodeService::CODE_OK;
            }
            return ReponseCodeService::CODE_FAILE;
        }catch(\Throwable $t){
            return ReponseCodeService::CODE_DB_ERROR;
        }
    }

    /**
     * 通过用户名登录
     */
    public function loginByUsername($username,$password){
        try{
            $adminModel = new AdminModel();
            $adminInfo  =  $adminModel ->get(['username'=>$username])->field('id,password_hash');
        }catch(\Throwable $t){
            return ReponseCodeService::CODE_DB_ERROR;
        }
         #校验密码
        if(empty( $adminInfo)||!$this->pwdCheck($password,$adminInfo['password_hash'])){
            return false;
        }

        return $this->login($adminInfo['id']);
    }

    /**
     * @param $adminId
     * @return mixed
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    function login($adminId){
        try{
            $adminModel = new AdminModel();
            $adminInfo  =  $adminModel ->get($adminId)->field('id,username,realname,head,mobile,created_at');
        }catch(\Throwable $t){
            return ReponseCodeService::CODE_DB_ERROR;
        }

        $jwtObject = Jwt::getInstance()
            //    ->setSecretKey('easyswoole') // 秘钥
            ->publish();

        $jwtObject->setAlg('HMACSHA256'); // 加密方式
        $jwtObject->setAud('admin'); // 用户
        $jwtObject->setExp(time()+86400*30); // 过期时间
        $jwtObject->setIat(time()); // 发布时间
        $jwtObject->setIss('easyswoole'); // 发行人
        $jwtObject->setJti(md5(time())); // jwt id 用于标识该jwt
        $jwtObject->setNbf(time()); // 在此之前不可用
        $jwtObject->setSub('用户信息'); // 主题
        // 自定义数据
        $jwtObject->setData( $adminInfo);

        // 最终生成的token
        $token = $jwtObject->__toString();
        $adminInfo['token'] = $token;
        return     $adminInfo;
    }


    /**
     * 密码加密
     */
    function pwdEncode($password){
        return password_hash($password, PASSWORD_DEFAULT);
    }

    /**
     * @param $password密码
     * @param $encodePassword密文
     */
    function pwdCheck($password,$encodedPassword){
        if(password_verify($password)==$encodedPassword){
            return true;
        }
        return false;
    }
}